package com.css.dzj.zfjg.modules.taskinfo.service.impl;

import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.fb.entity.JgFbLhjgEntity;
import com.css.dzj.zfjg.modules.fb.mapper.JgFbLhjgMapper;
import com.css.dzj.zfjg.modules.taskinfo.entity.JgLhjgRwsEntity;
import com.css.dzj.zfjg.modules.taskinfo.entity.QJgLhjgRwsEntity;
import com.css.dzj.zfjg.modules.taskinfo.mapper.TaskInfoMapper;
import com.css.dzj.zfjg.modules.taskinfo.service.TaskInfoService;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 任务书serviceImpl
 */
@Service
public class TaskInfoServiceImpl implements TaskInfoService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private JgFbLhjgMapper fenFaMapper;


    /**
     * 添加任务书
     * @param jgLhjgRwsEntity
     */
    @Transactional
    @Override
    public void add(JgLhjgRwsEntity jgLhjgRwsEntity, HttpServletRequest request) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgLhjgRwsEntity.setCreateUser(userInfo.getLoginName());
            }
            jgLhjgRwsEntity.setCreateTime(new Date());
            taskInfoMapper.save(jgLhjgRwsEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改任务书
     * @param jgLhjgRwsEntity
     */
    @Transactional
    @Override
    public void update(JgLhjgRwsEntity jgLhjgRwsEntity) {

    }

    /**
     * 通过id查找任务书
     * @param id
     * @return
     */
    @Override
    public JgLhjgRwsEntity findById(String id) {
        return taskInfoMapper.findById(id).get();
    }


    /**
     * 分页查询所有任务书
     * @return
     */
    @Override
    public QueryResults<JgLhjgRwsEntity> queryAll(Pageable pageable) {
        QJgLhjgRwsEntity qJgLhjgRwsEntity = QJgLhjgRwsEntity.jgLhjgRwsEntity;
        QueryResults<JgLhjgRwsEntity> taskInfoQueryResults = jpaQueryFactory.selectFrom(qJgLhjgRwsEntity)
                .orderBy(qJgLhjgRwsEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return taskInfoQueryResults;
    }

    /**
     * 多条件分页查询任务书
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<JgLhjgRwsEntity> listTaskInfo(Pageable pageable,String taskName,String superviseObjectType,String superviseObjectName,String taskStartTimeStart,String taskStartTimeEnd,String status) {
        QJgLhjgRwsEntity qJgLhjgRwsEntity = QJgLhjgRwsEntity.jgLhjgRwsEntity;
        Predicate predicate = qJgLhjgRwsEntity.isNotNull().or(qJgLhjgRwsEntity.isNull());
        predicate = (taskName == null || taskName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgRwsEntity.taskname.like("%" + taskName + "%"));
        predicate = (status == null || status.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgRwsEntity.taskState.eq(status));
        if ((StringUtils.isEmpty(taskStartTimeStart)) && (!StringUtils.isEmpty(taskStartTimeEnd))) {
            Date startDate = new Date();
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                endDate = sf.parse(taskStartTimeEnd + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qJgLhjgRwsEntity.taskstarttime.between(startDate, endDate));
        }
        if ((!StringUtils.isEmpty(taskStartTimeStart)) && (StringUtils.isEmpty(taskStartTimeEnd))) {
            Date startDate = null;
            Date endDate = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(taskStartTimeStart+" "+"00:00:00.000");

            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qJgLhjgRwsEntity.taskstarttime.between(startDate, endDate));
        }
        if ((!StringUtils.isEmpty(taskStartTimeStart)) && (!StringUtils.isEmpty(taskStartTimeEnd))) {
            Date startDate = null;
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(taskStartTimeStart+" "+"00:00:00.000");
                endDate = sf.parse(taskStartTimeEnd + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qJgLhjgRwsEntity.taskstarttime.between(startDate, endDate));
        }

        QueryResults<JgLhjgRwsEntity> taskInfoQueryResults = jpaQueryFactory.selectFrom(qJgLhjgRwsEntity)
                .where(predicate)
                .orderBy(qJgLhjgRwsEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return taskInfoQueryResults;
    }





    /**
     * 删除任务书
     * @param jgLhjgRwsEntity
     */
    @Override
    public void delete(JgLhjgRwsEntity jgLhjgRwsEntity) {
        try {
            taskInfoMapper.delete(jgLhjgRwsEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 分发任务书
     * @param jgFbLhjgEntity
     */
    @Transactional
    @Override
    public void fenfa(JgFbLhjgEntity jgFbLhjgEntity, HttpServletRequest request) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgFbLhjgEntity.setCreateUser(userInfo.getLoginName());
            }
            jgFbLhjgEntity.setCreateTime(new Date());
            fenFaMapper.save(jgFbLhjgEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
